package com.aluvfy.jdbc;

import com.aluvfy.jdbc.beans.Employee;
import com.aluvfy.jdbc.dao.EmployeeDao;

import java.util.List;
import java.util.Scanner;

/**
 * @author: aluvfy
 * @description: 员工信息管理-使用DAO改造
 * @date: 2024/10/21 18:50
 */
public class JDBCTest23 {
    public static void main(String[] args) {
        System.out.println("欢迎使用员工信息管理，请认真阅读使用说明！");
        System.out.println("本系统的主要功能包括：查看员工列表、查看某个员工信息、新增员工、修改员工、删除员工");
        System.out.println("请输入对应的功能编号，选择功能：");
        System.out.println("1.查看员工列表");
        System.out.println("2.查看某个员工信息");
        System.out.println("3.新增员工");
        System.out.println("4.修改员工");
        System.out.println("5.删除员工");
        System.out.println("0.退出系统");

        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("请输入功能编号：");
            int no = scanner.nextInt();
            if (no == 0) {
                System.out.println("欢迎下次使用！");
                break;
            } else if (no == 1) {
                //查看员工列表
                doList();
            } else if (no == 2) {
                //查看某个员工信息
                doList();
                System.out.println("请输入要查看的员工编号：");
                long id = scanner.nextLong();
                doDetail(id);
            } else if (no == 3) {
                //新增员工
                doAdd();
            } else if (no == 4) {
                //修改员工
                doModify();
            } else if (no == 5) {
                //删除员工
                doDelete();
            } else {
                System.out.println("输入的功能编号有误，请重新输入！");
            }
        }

    }
    private static EmployeeDao employeeDao= new EmployeeDao();
    private static void doDelete() {
        doList();
        System.out.println("请输入要删除的员工编号：");
        Scanner scanner = new Scanner(System.in);
        long id = scanner.nextLong();

//        EmployeeDao employeeDao = new EmployeeDao();
        employeeDao.deleteById(id);
    }

    private static void doModify() {
        doList();
        System.out.println("请输入您要修改的员工编号：");
        Scanner scanner = new Scanner(System.in);
        long id = scanner.nextLong();

        doDetail(id);
        System.out.println("请输入员工姓名：");
        String name = scanner.next();
        System.out.println("请输入员工职位：");
        String job = scanner.next();
        System.out.println("请输入入职日期：");
        String hiredate = scanner.next();
        System.out.println("请输入员工薪资：");
        double salary = scanner.nextDouble();
        System.out.println("请输入员工住址：");
        String address = scanner.next();

//        EmployeeDao employeeDao = new EmployeeDao();
        Employee employee = new Employee(id, name, job, salary, hiredate, address);
        employeeDao.update(employee);
    }

    private static void doAdd() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入员工姓名：");
        String name = scanner.next();
        System.out.println("请输入员工职位：");
        String job = scanner.next();
        System.out.println("请输入入职日期：");
        String hiredate = scanner.next();
        System.out.println("请输入员工薪资：");
        Double salary = scanner.nextDouble();
        System.out.println("请输入员工住址：");
        String address = scanner.next();

//        EmployeeDao employeeDao = new EmployeeDao();
        Employee employee = new Employee(name, job, salary, hiredate, address);
        employeeDao.insert(employee);
    }

    private static void doDetail(long id) {
//        EmployeeDao employeeDao = new EmployeeDao();
        Employee employee = employeeDao.selectById(id);
//        employee.toString();
        System.out.println("id1 = " + employee.getId());
        System.out.println("name = " + employee.getName());
        System.out.println("job = " + employee.getJob());
        System.out.println("hiredate = " + employee.getHiredate());
        System.out.println("salary = " + employee.getSalary());
        System.out.println("address = " + employee.getAddress());

    }

    private static void doList() {
        System.out.println("员工信息列表如下：");
//        EmployeeDao employeeDao = new EmployeeDao();
        List<Employee> employees = employeeDao.selectAll();
        System.out.println("id\tname\tjob");
        System.out.println("--------------------------------");
        for(Employee e : employees) {
            System.out.println(e.getId() + "\t" + e.getName() + "\t" + e.getJob());
        }
    }
}
